package com.alan.camunda.boot.controller;

import jakarta.annotation.Resource;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.repository.Deployment;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yaoyunqing
 * @date 2020/12/05
 */
@RequestMapping("/deployment")
@RestController
public class DeploymentManageController {
    @Resource
    private RepositoryService repositoryService;
    @Resource
    private RuntimeService runtimeService;


    @PostMapping("/teriminateAll")
    public String teriminateAll() {
        // 删除所有运行中的流程
        List<ProcessInstance> list = runtimeService.createProcessInstanceQuery().active().list();
        for (ProcessInstance processInstance : list) {
            // 这个查询保证流程还存在，避免删除报错
            long count = runtimeService.createProcessInstanceQuery().active().processInstanceId(processInstance.getProcessInstanceId()).count();
            if (count == 1) {
                // 这里的第三个参数 skipIoMappings 为 true，如果输出变量无法计算，不会抛出异常
                runtimeService.deleteProcessInstance(processInstance.getProcessInstanceId(), "Clear All",
                        true, true, true);
            }
        }

        return "已删除所有流程实例";
    }


    @GetMapping("/byName/{name}")
    public List<Deployment> findByName(@PathVariable("name") String name) {
        // 根据部署名称查询部署信息
        List<Deployment> deploymentList = repositoryService.createDeploymentQuery().deploymentName(name).list();
        String deploymentId = deploymentList.get(0).getId();
        List<ProcessDefinition> definitions = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).list();
        String engineDefId = definitions.get(0).getId();

        return deploymentList;
    }


    @GetMapping("/cleanup")
    public String cleanup() {
        // 删除所有运行中的流程
        List<ProcessInstance> list = runtimeService.createProcessInstanceQuery().active().list();
        for (ProcessInstance processInstance : list) {
            // 这个查询保证流程还存在，避免删除报错
            long count = runtimeService.createProcessInstanceQuery().active().processInstanceId(processInstance.getProcessInstanceId()).count();
            if (count == 1) {
                // 这里的第三个参数 skipIoMappings 为 true，如果输出变量无法计算，不会抛出异常
                runtimeService.deleteProcessInstance(processInstance.getProcessInstanceId(), "Clear All",
                        true, true, true);
            }
        }
        // 删除所有部署
        List<Deployment> deploymentList = repositoryService.createDeploymentQuery().list();
        List<String> deployIds = deploymentList.stream().map(Deployment::getId).toList();
        for (String deployId : deployIds) {
            repositoryService.deleteDeployment(deployId, true);
        }
        return "已清空所有部署的流程";
    }


    @GetMapping("/delete/{id}")
    public String terminateAll(@PathVariable("id") String id) {
        // 根据ID删除指定的部署
        repositoryService.deleteDeployment(id);
        return "SUCCESS";
    }

}
